package com.happyclub.paul;

import com.happyclub.paul.service.PhotoAlbumService;
import com.happyclub.pojo.TbPhotoAlbum;
import com.happyclub.pojo.TbPhotoLibrary;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: 魏泉琦
 * @Date: 2019/4/3 15:02
 * @Description:  相册
 */
@Controller
public class PhotoAlbumController {

    @Autowired
    private PhotoAlbumService photoAlbumService;

    /**
     * @Description: 当用户点击创建相册时，回到创建相册页面
     * @Author: 魏泉琦
     * @date: 2019/4/3
     */
    @RequestMapping(value = "/backPhotoAlbumHtmml")
    public String backPhotoAlbumHtmml() {
        return "paul-html/photoalbum";
    }


    /**
     * @Description: 保存用户创建的相册信息
     * @Author: 魏泉琦
     * @date: 2019/4/3
     */
    @ResponseBody
    @RequestMapping(value = "/createPhotoAlbum/{photoAlbumName}")
    public JsonResult createPhotoAlbum(@PathVariable("photoAlbumName") String photoAlbumName, HttpServletRequest request) {
        System.out.println(photoAlbumName);
        //从session获取登录用户
        /*
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("user");
        */

        //测试 自定义用户编号传入     97fd7504-2253-4ee5-b049-d1cbcbff147f
        //  return photoAlbumService.saveUserPhotoAlbumMsg(user.getUserId(), photoAlbumName);
        return photoAlbumService.saveUserPhotoAlbumMsg("97fd7504-2253-4ee5-b049-d1cbcbff147f", photoAlbumName);
    }


    /**
     * @Description: 通过相册编号删除对应相册
     * @Author: 魏泉琦
     * @date: 2019/4/6
     */
    @ResponseBody
    @RequestMapping(value = "deletePhotoAlbum")
    public JsonResult deletePhotoAlbum(@RequestParam("photoAlbumId") String photoAlbumId) {
        //先删除用户相册中间表数据
        //再删除主表 相册表
        return photoAlbumService.deletePhotoAlbumById(photoAlbumId);
    }


    /**
     * @Description: 查看相册详情  点击进入相册
     * @Author: 魏泉琦
     * @date: 2019/4/7
     */
    @RequestMapping(value = "lookPhotoAlbum/{photoAlbumId}")
    public String newPhoto(@PathVariable("photoAlbumId") String photoAlbumId, Model model) {
        //获取到了相册的编号，先查询该相册是否拥有相片
        JsonResult jsonResult = photoAlbumService.queryPhotoAlbumEveryPhoto(photoAlbumId);
        TbPhotoAlbum result =(TbPhotoAlbum) jsonResult.getResult();
        List<TbPhotoLibrary> photoLibraries = result.getPhotoLibraries();
        model.addAttribute("jsonResult", jsonResult);  //保存要增加相片的相册编号
        return "paul-html/lookPhotoAlbum";

    }


    /**
     * @Description: 上传相片
     * @Author: 魏泉琦
     * @date: 2019/4/8
     */
    @ResponseBody
    @PostMapping(value = "uploadPhotoFile")
    public JsonResult uploadPhotoFile(@RequestParam("albumId") String albumId, @RequestParam("uploadFile") MultipartFile[] multipartFiles, HttpServletRequest request) {
        /*
        TbUser user =(TbUser) request.getSession().getAttribute("user");
        return photoAlbumService.uploadPhotoAlbumFile(albumId,multipartFile,user.getUserId());
        */

        //测试
        return photoAlbumService.uploadPhotoAlbumFile(albumId,multipartFiles,"fa95b5cfd9ef4d0a94e0a00c0b352436",null);
    }



    /**
     *@Description: 查询要修改的相册信息，回显
     *@Author: 魏泉琦
     *@date: 2019/4/9
     */
    @ResponseBody
    @RequestMapping(value = "queryEditAlbumNameByAlbumId/{albumId}")
    public JsonResult queryEditAlbumNameByAlbumId(@PathVariable("albumId") String albumId){
        return photoAlbumService.queryEditAlbumNameByAlbumId(albumId);
    }

    /**
     *@Description: 保存修改之后的相册信息
     *@Author: 魏泉琦
     *@date: 2019/4/9
     */
    @ResponseBody
    @RequestMapping(value = "saveEditAlbumName/{albumId}/{newAlbumName}")
    public JsonResult saveEditAlbumName(@PathVariable("albumId") String albumId,@PathVariable("newAlbumName") String newAlbumName){
       return   photoAlbumService.saveEditAlbumName(albumId, newAlbumName);
    }
}

